###############################
#   take bootstrapped files   #
# from parallel habanero runs #
#   and merge into matrices   #
###############################

rm(list=ls())

setwd('~/../../Class+Party/')

library(tidyverse)

tmp <- readRDS('opinion_bootstrapped_noleaners.RDS')

###  CLASS THRESHOLDS  ###

opiniondfs <- expand.grid(party = c('rep', 'dem', 'all'),
                          var = c('inctop', 'incmiddle', 'incbottom'))


results_classthresh <- lapply(dir('data/bootstraps_out/classthresholds/', full.names = T), readRDS)
results_classthresh <- unlist(results_classthresh, recursive = FALSE)

# make output dfs 

dfnames <- unique(gsub('_sim_[0-9]{1,4}$', '', names(results_classthresh)))

for (d in dfnames) {
  assign(d, matrix(NA, nrow = length(results_classthresh[[1]]), ncol = 1000))
}

for (i in 1:length(results_classthresh)) {
  if (i %% 1000 == 0) print(i)
  tmp.name <- names(results_classthresh[i])
  tmp.dfname <- gsub('_sim_[0-9]{1,4}$', '', tmp.name)
  tmp.df <- get(tmp.dfname)
  tmp.sim <- as.numeric(gsub(paste0(tmp.dfname, '_sim_'), '', tmp.name))-1
  
  tmp.df[,tmp.sim] <- results_classthresh[[i]]
  
  assign(tmp.dfname, tmp.df)
}



output <- list()

output$issuetopics <- tmp$issuetopics

for (d in dfnames) {
  tmp.df <- get(d)
  rownames(tmp.df) <- sort(output$issuetopics$question)
  output[[d]] <- tmp.df
}

saveRDS(output, 'opinion_bootstrapped_classthresholds.RDS')


###  WITHIN PARTY  ###

opiniondfs <- expand.grid(party = c('rep', 'dem'),
                          var = c('inctop', 'incmiddle', 'incbottom'))


results_withinparty <- lapply(dir('data/bootstraps_out/withinparty/', full.names = T), readRDS)
results_withinparty <- unlist(results_withinparty, recursive = FALSE)

# make output dfs 

dfnames <- unique(gsub('_sim_[0-9]{1,4}$', '', names(results_withinparty)))

for (d in dfnames) {
  assign(d, matrix(NA, nrow = length(results_withinparty[[1]]), ncol = 1000))
}

for (i in 1:length(results_withinparty)) {
  if (i %% 1000 == 0) print(i)
  tmp.name <- names(results_withinparty[i])
  tmp.dfname <- gsub('_sim_[0-9]{1,4}$', '', tmp.name)
  tmp.df <- get(tmp.dfname)
  tmp.sim <- as.numeric(gsub(paste0(tmp.dfname, '_sim_'), '', tmp.name))-1
  
  tmp.df[,tmp.sim] <- results_withinparty[[i]]
  
  assign(tmp.dfname, tmp.df)
}


output <- list()

output$issuetopics <- tmp$issuetopics

for (d in dfnames) {
  tmp.df <- get(d)
  rownames(tmp.df) <- sort(output$issuetopics$question)
  output[[d]] <- tmp.df
}

saveRDS(output, 'opinion_bootstrapped_withinparty.RDS')



###  CLASS X URBAN  ###

results_classurban <- lapply(dir('data/bootstraps_out/classbyurban/', full.names = T), readRDS)
results_classurban <- unlist(results_classurban, recursive = FALSE)

# make output dfs 

dfnames <- unique(gsub('_sim_[0-9]{1,4}$', '', names(results_classurban)))

for (d in dfnames) {
  assign(d, matrix(NA, nrow = length(results_classurban[[1]]), ncol = 1000))
}

for (i in 1:length(results_classurban)) {
  if (i %% 1000 == 0) print(i)
  tmp.name <- names(results_classurban[i])
  tmp.dfname <- gsub('_sim_[0-9]{1,4}$', '', tmp.name)
  tmp.df <- get(tmp.dfname)
  tmp.sim <- as.numeric(gsub(paste0(tmp.dfname, '_sim_'), '', tmp.name))-1
  
  tmp.df[,tmp.sim] <- results_classurban[[i]]
  
  assign(tmp.dfname, tmp.df)
}


output <- list()

output$issuetopics <- tmp$issuetopics %>% filter(year %in% 2018:2020)

for (d in dfnames) {
  tmp.df <- get(d)
  rownames(tmp.df) <- sort(output$issuetopics$question)
  output[[d]] <- tmp.df
}

saveRDS(output, 'opinion_bootstrapped_classbyurban.RDS')



###  WHITES  ###


results_whites <- lapply(dir('data/bootstraps_out/whites/', full.names = T), readRDS)
results_whites <- unlist(results_whites, recursive = FALSE)

# make output dfs 

dfnames <- unique(gsub('_sim_[0-9]{1,4}$', '', names(results_whites)))

for (d in dfnames) {
  assign(d, matrix(NA, nrow = length(results_whites[[1]]), ncol = 1000))
}

for (i in 1:length(results_whites)) {
  if (i %% 1000 == 0) print(i)
  tmp.name <- names(results_whites[i])
  tmp.dfname <- gsub('_sim_[0-9]{1,4}$', '', tmp.name)
  tmp.df <- get(tmp.dfname)
  tmp.sim <- as.numeric(gsub(paste0(tmp.dfname, '_sim_'), '', tmp.name))-1
  
  tmp.df[,tmp.sim] <- results_whites[[i]]
  
  assign(tmp.dfname, tmp.df)
}


output <- list()

output$issuetopics <- tmp$issuetopics

for (d in dfnames) {
  tmp.df <- get(d)
  rownames(tmp.df) <- sort(output$issuetopics$question)
  output[[d]] <- tmp.df
}

saveRDS(output, 'opinion_bootstrapped_noleaners_whites.RDS')



###  LEANERS  ###

results_threshwithleaners <- lapply(dir('data/bootstraps_out/leanerclassthresholds/', full.names = T), readRDS)
results_threshwithleaners <- unlist(results_threshwithleaners, recursive = FALSE)

# make output dfs 

dfnames <- unique(gsub('_sim_[0-9]{1,4}$', '', names(results_threshwithleaners)))

for (d in dfnames) {
  assign(d, matrix(NA, nrow = length(results_threshwithleaners[[1]]), ncol = 1000))
}

for (i in 1:length(results_threshwithleaners)) {
  if (i %% 1000 == 0) print(i)
  tmp.name <- names(results_threshwithleaners[i])
  tmp.dfname <- gsub('_sim_[0-9]{1,4}$', '', tmp.name)
  tmp.df <- get(tmp.dfname)
  tmp.sim <- as.numeric(gsub(paste0(tmp.dfname, '_sim_'), '', tmp.name))-1
  
  tmp.df[,tmp.sim] <- results_threshwithleaners[[i]]
  
  assign(tmp.dfname, tmp.df)
}


output <- list()

output$issuetopics <- tmp$issuetopics

for (d in dfnames) {
  tmp.df <- get(d)
  rownames(tmp.df) <- sort(output$issuetopics$question)
  output[[d]] <- tmp.df
}

saveRDS(output, 'opinion_bootstrapped_thresholdwithleaners.RDS')


